package com.dmtt.web.common;

import com.dmtt.web.mapper.CarStatusMapper;
import com.dmtt.web.service.StatisticsService;
import com.dmtt.web.service.impl.CarStatusServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Configuration
@Component
@EnableScheduling
public class AgvSchouled {
    @Autowired
    private CarStatusServiceImpl carStatusService;

    @Autowired
    private StatisticsService statisticsService;

    @Autowired
    private CarStatusMapper carStatusMapper;

    @Transactional
    public void statiscsRate(String time){
        String startTime = time + " 00:00:00";
        String endTime = time + " 23:59:59";

        //凌晨12点更新所有车辆状态为调试
        carStatusService.agvStatusUpdate(endTime);

        statisticsService.grainRate(startTime,endTime);
//        statisticsService.grainRateCount(startTime,endTime);
    }

    @Scheduled(cron = "0 0 0/1 * * ?")
    @Async
    public void staticsRateTemp(){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1=Calendar.getInstance();
        calendar1.setTime(new Date());
        calendar1.add(Calendar.HOUR_OF_DAY,0);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = simpleDateFormat1.format(calendar.getTime());
        String endTime = simpleDateFormat.format(calendar1.getTime());

        statisticsService.currDayGrainCount(startTime + " 00:00:00",endTime);
//        statisticsService.currDayGrainTotal(startTime,endTime);
    }

    public void oldCar(){
        carStatusService.oldCar();
        carStatusService.findAgv();
    }

    @Scheduled(cron = "0/5 * * * * ?")
    @Async
    public void scheduledTest(){
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String startTime = simpleDateFormat1.format(calendar.getTime());
            int hour = Integer.parseInt(startTime.substring(11, 13));
            int minute = Integer.parseInt(startTime.substring(14, 16));
            int second = Integer.parseInt(startTime.substring(17, 19));
//            if (hour % 23 == 0 && minute % 59 == 0) {
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//                String time = simpleDateFormat.format(calendar.getTime());
//                Integer flag = carStatusMapper.intFalg(time);
//                if (flag == 0) {
//                    statiscsRate(startTime.substring(0, 10));
//                }
//            } else
            if (minute % 59 == 0 && second % 40 < 20) {
                staticsRateTemp();
            }
            if (hour == 0 && minute == 0) {
                return;
            }
            carStatusService.oldCar();
            carStatusService.findAgv();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "0 0 0 * * ?")
    @Async
    public void scheduledTest1(){
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String startTime = simpleDateFormat.format(calendar.getTime());
            Integer flag = carStatusMapper.intFalg(startTime);
            if (flag == 0) {
                statiscsRate(startTime);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "0 0 5 * * ?")
    @Async
    public void scheduledYearCalendar(){
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String startTime = simpleDateFormat.format(calendar.getTime());
            statisticsService.deleteStatics(startTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




